    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.util.pager;

import java.util.Collection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.ipo.mm.ejb.entities.Meeting;
import org.ipo.mm.ejb.facade.meeting.MeetingFacadeLocal;

/**
 *
 * @author makcro
 */
public class MeetingPager extends AbstractPager {

    private final String meetingNameFilter;
    private final String meetingNameOper;
    private final String venueFilter;
    private final String venueOper;
    private final String conclutionsFilter;
    private final String conclutionsOper;
    private final Date startDate;
    private final Date endDate;
    private final String orgNameFilter;
    private final String orgNameOper;
    private final String[] orgInstFilter;
    private final String[] orgITypeFilter;
    private final String[] orgIFamilyFilter;
    private final String[] orgIRegionFilter;
    private final String[] orgIdFilter;
    private final String repNameFilter;
    private final String repNameOper;
    private final String[] repInstFilter;
    private final String[] repITypeFilter;
    private final String[] repIFamilyFilter;
    private final String[] repIRegionFilter;
    private final String[] repIdFilter;
    private final String orderByFilter;
    private final boolean isQuickSearch;

    public MeetingPager(
            String meetingNameFilter,
            String meetingNameOper,
            String venueFilter,
            String venueOper,
            String conclutionsFilter,
            String conclutionsOper,
            Date startDate,
            Date endDate,
            String orgNameFilter,
            String orgNameOper,
            String[] orgInstFilter,
            String[] orgITypeFilter,
            String[] orgIFamilyFilter,
            String[] orgIRegionFilter,
            String[] orgIdFilter,
            String repNameFilter,
            String repNameOper,
            String[] repInstFilter,
            String[] repITypeFilter,
            String[] repIFamilyFilter,
            String[] repIRegionFilter,
            String[] repIdFilter,
            String orderByFilter,
            boolean isQuickSearch) {

        this.meetingNameFilter = meetingNameFilter;
        this.meetingNameOper = meetingNameOper;
        this.venueFilter = venueFilter;
        this.venueOper = venueOper;
        this.conclutionsFilter = conclutionsFilter;
        this.conclutionsOper = conclutionsOper;
        this.startDate = startDate;
        this.endDate = endDate;
        this.orgNameFilter = orgNameFilter;
        this.orgNameOper = orgNameOper;
        this.orgInstFilter = orgInstFilter;
        this.orgITypeFilter = orgITypeFilter;
        this.orgIFamilyFilter = orgIFamilyFilter;
        this.orgIRegionFilter = orgIRegionFilter;
        this.repNameFilter = repNameFilter;
        this.repNameOper = repNameOper;
        this.repInstFilter = repInstFilter;
        this.repITypeFilter = repITypeFilter;
        this.repIFamilyFilter = repIFamilyFilter;
        this.repIRegionFilter = repIRegionFilter;
        this.repIdFilter = repIdFilter;
        this.orgIdFilter = orgIdFilter;
        this.orderByFilter = orderByFilter;
        this.isQuickSearch = isQuickSearch;
        this.resultsTotal = lookupMeetingFacade().getSearchResultsCount(
                meetingNameFilter,
                meetingNameOper,
                venueFilter,
                venueOper,
                conclutionsFilter,
                conclutionsOper,
                startDate,
                endDate,
                orgNameFilter,
                orgNameOper,
                orgInstFilter,
                orgITypeFilter,
                orgIFamilyFilter,
                orgIRegionFilter,
                orgIdFilter,
                repNameFilter,
                repNameOper,
                repInstFilter,
                repITypeFilter,
                repIFamilyFilter,
                repIRegionFilter,
                repIdFilter,
                orderByFilter,
                isQuickSearch);
    }

    @Override
    protected void injectData(HttpServletRequest request) {

        Collection<Meeting> meetings = lookupMeetingFacade().search(
                meetingNameFilter,
                meetingNameOper,
                venueFilter,
                venueOper,
                conclutionsFilter,
                conclutionsOper,
                startDate,
                endDate,
                orgNameFilter,
                orgNameOper,
                orgInstFilter,
                orgITypeFilter,
                orgIFamilyFilter,
                orgIRegionFilter,
                orgIdFilter,
                repNameFilter,
                repNameOper,
                repInstFilter,
                repITypeFilter,
                repIFamilyFilter,
                repIRegionFilter,
                repIdFilter,
                orderByFilter,
                firstResult,
                maxResultsPerPage,
                isQuickSearch);

        request.setAttribute("MEETING_SEARCH_RESULTS", meetings);
        request.setAttribute("MEETING_SEARCH_PAGENUMBERS", generatePageNumbers());
        request.setAttribute("MEETING_SEARCH_TOTAL_RESULTS", this.resultsTotal);
        request.setAttribute("MEETING_SEARCH_SHOW_NEXT", new Boolean(this.showNext));
        request.setAttribute("MEETING_SEARCH_SHOW_PREV", new Boolean(this.showPrev));

    }

    @Override
    protected void setPageSize() {
        maxResultsPerPage = 20;
    }

    private MeetingFacadeLocal lookupMeetingFacade() {
        try {
            Context c = new InitialContext();
            return (MeetingFacadeLocal) c.lookup("java:comp/env/MeetingFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
